﻿using DtuService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DtuService
{
    public class TokenInfo
    {
        public string tokenString;
        public uint userID;
        public long timeStamp;
        public string clientIP;
    }

    public class RegistCodeInfo
    {
        public string PhoneNumber;
        public string RegCode;
        public long timeStamp;
    }

    public class Session
    {
        private Dictionary<string, object> sessionDictionary = new Dictionary<string, object>();
        public long TimeStamp;
        public object this[string index]
        {
            get
            {
                if (sessionDictionary.ContainsKey(index))
                {
                    return sessionDictionary[index];
                }
                return null;
            }
            set
            {
                sessionDictionary[index] = value;
            }

        }

    }

    public class CacheServer : IServer
    {
        private long timeOutTime = 1000 * 60 * 30; //   30 分
        private Dictionary<string, TokenInfo> tokenDictionary = new Dictionary<string, TokenInfo>();
        public Dictionary<string, Session> SessionDintionary = new Dictionary<string, Session>();
        public Dictionary<string, RegistCodeInfo> PhoneRegistCodeMap = new Dictionary<string, RegistCodeInfo>();
        public void Init()
        {
           
        }

        public void Tick(float deltaTime)
        {
            // 清除过期的缓存
            long time = ServerMain.Instance.CommonTools.GetTimeStamp();
            Dictionary<string, TokenInfo> tempToken = new Dictionary<string, TokenInfo>(tokenDictionary);
            foreach (KeyValuePair<string, TokenInfo> token in tempToken)
            {
                if (time - token.Value.timeStamp >= timeOutTime)
                { // 超时了
                    tokenDictionary.Remove(token.Key);
                }
            }
            Dictionary<string, Session> tempSession = new Dictionary<string, Session>(SessionDintionary);
            foreach (KeyValuePair<string, Session> session in tempSession)
            {
                if (time - session.Value.TimeStamp >= timeOutTime)
                { // 超时了
                    SessionDintionary.Remove(session.Key);
                }
            }
            foreach(KeyValuePair<string, RegistCodeInfo> regcode in PhoneRegistCodeMap)
            {
                if (time - regcode.Value.timeStamp >= timeOutTime)
                { // 超时了
                    SessionDintionary.Remove(regcode.Key);
                }
            }
        }

        /// <summary>
        /// 删除用户缓存
        /// </summary>
        /// <param name="token"></param>
        public void DeleteCache(string token)
        {
            if (tokenDictionary.ContainsKey(token))
            {
                string ipAddress = tokenDictionary[token].clientIP;
                if (SessionDintionary.ContainsKey(ipAddress))
                {
                    SessionDintionary.Remove(ipAddress);
                }
                tokenDictionary.Remove(token);
            }
        }

        public Session GetSecssion(string token)
        {
            if (SessionDintionary.ContainsKey(token))
            {
                return SessionDintionary[token];
            }
            return null;
        }

        public void UpdateTokenTime(string token)
        {
            if (token == null)
                return;
            if (tokenDictionary.ContainsKey(token))
            {
                TokenInfo tk = tokenDictionary[token];
                tk.timeStamp = ServerMain.Instance.CommonTools.GetTimeStamp();
                if (SessionDintionary.ContainsKey(tk.clientIP))
                {
                    SessionDintionary[tk.clientIP].TimeStamp = tk.timeStamp;
                }
            }
        }

        public void ShutDown()
        {
             
        }
        public bool TokenIsLegal(string token)
        {
            return tokenDictionary.ContainsKey(token);
        }

        public TokenInfo GetTokenInfo(string tokenStr)
        {
            if (TokenIsLegal(tokenStr))
            {
                return tokenDictionary[tokenStr];
            }
            return null;
        }

        public void AddPhoneRegCode(string phone, string code)
        {
            RegistCodeInfo info = new RegistCodeInfo();
            info.PhoneNumber = phone;
            info.RegCode = code;
            info.timeStamp = ServerMain.Instance.CommonTools.GetTimeStamp();
            PhoneRegistCodeMap[phone] = info;
        }

        public bool CheckIdentityCode(string phone, string code)
        {
            if (!PhoneRegistCodeMap.ContainsKey(phone))
            {
                return false;
            }
            if (PhoneRegistCodeMap[phone].RegCode.ToLower() != code.ToLower())
            {
                return false;
            }

            return true;
        }

        public string CreateTokenWithUserID(uint userID, string ip)
        {
            string token = ServerMain.Instance.CommonTools.GenTokenChar();
            TokenInfo info = new TokenInfo();
            info.userID = userID;
            
            info.tokenString = token;
            info.clientIP = ip;
            info.timeStamp = ServerMain.Instance.CommonTools.GetTimeStamp();
            tokenDictionary[token] = info;
            return token;
        }

    }

}
